package kr.co.bettersoft.pushchat.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import kr.co.bettersoft.pushchat.common.PushChatRest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * SearchMemberActivity
 *  멤버 검색.
 *   상단 : 회원 검색. / 좌측에 닫기 버튼
 *   회원 아이디 : 입력 받기
 *   닉네임 : 입력 받기
 *   All / 남성 / 여성 / 기타
 *   All / 10 / 20 / 30/ 40/ 50/ 60
 *   성격 선택(스피너)
 *   하단 검색 버튼 같은거
 *   
 * @author blue
 *
 */
public class SearchMemberActivity extends Activity  
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	//변수
	String TAG = "SearchStoreOptionActivity";

	// 종료 여부. 종료된 경우 스레드 호출로 인한 에러를 방지하기 위함
	Boolean isFinished = false;

	public String COLOR_SELECTED = "#ff0000";
	public String COLOR_NOT_SELECTED = "#000000";



	// 서버 통신용
	PushChatRest pushChatRest;
	String callResult = "";

	// 화면 구성
	// 아이디, 닉네임 입력창
	EditText memberIdSearchText;
	EditText memberNickNameSearchText;

	Button member_search_gender_all_btn;			// 성별ALL
	Button member_search_gender_male_btn;			// 성별 남
	Button member_search_gender_female_btn;			// 성별 여
	Button member_search_gender_etc_btn;			// 성별 기타
	Button member_search_age_all_btn;			// 나이 ALL
	Button member_search_age_10_btn;			// 나이 10
	Button member_search_age_20_btn;			// 나이 20
	Button member_search_age_30_btn;			// 나이 30
	Button member_search_age_40_btn;			// 나이 40
	Button member_search_age_50_btn;			// 나이 50
	Button member_search_age_60_btn;			// 나이 60
	String selectedGender = "";
	String selectedAge = "";



	// 상단 닫기 버튼
	Button topNaviCloseBtn; 
	// 하단 검색 버튼
	Button callSearchBtn; 

	// 데이터 전달용 번들과 인텐트
	Bundle extraBundle;
	Intent resultIntent;

	// 키보드 자동 숨기기 위한 부모 레이아웃(리스너 달아서 키보드 숨김)과 입력 매니저
	View parentLayout;
	InputMethodManager imm;		// 키보드 제어

	///////////////////////////////////////////////////////////////////////////////////////////////////////////

	// 핸들러
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			Bundle b = msg.getData();
			try{
				//				// 그리드뷰에서 업종 선택시 배경색상을 변경해준다.
				//				if(b.getInt("gridViewChangeBackgroundColor")==1){	
				//					for(int i=0; i<gridView.getChildCount(); i++){
				//						if(i==selectedBusinessKindIdx){
				//							gridView.getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.sub9_9_1_3));
				//						}else{
				//							gridView.getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.sub9_9_2_3));	
				//						}
				//					}
				//				}
				//				// 그리드뷰 설정 후 '전체' 버튼을 기본 선택 상태로 바꾼다.
				//				if(b.getInt("businessKindInitColor")==1){		 
				//					Log.d(TAG,"businessKindInitColor");
				//					try{
				//						selectedBusinessKindTag = "";	// 초기값 ""
				//						selectedBusinessKindIdx = 0;	// 초기값 0
				//						gridView.getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.sub9_9_1_3));
				//					}catch(Exception e){}
				//				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		setContentView(R.layout.search_member_activity);

		//		pushChatRest = new PushChatRest();

		// 부모 레이아웃 리스너 - 외부 터치 시 키보드 숨김 용도 
		parentLayout = findViewById(R.id.parent_layout);		
		parentLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideKeyboard();
			}
		});
		member_search_gender_all_btn = (Button) findViewById(R.id.member_search_gender_all_btn);
		member_search_gender_male_btn = (Button) findViewById(R.id.member_search_gender_male_btn);
		member_search_gender_female_btn = (Button) findViewById(R.id.member_search_gender_female_btn);
		member_search_gender_etc_btn = (Button) findViewById(R.id.member_search_gender_etc_btn);
		member_search_age_all_btn = (Button) findViewById(R.id.member_search_age_all_btn);
		member_search_age_10_btn = (Button) findViewById(R.id.member_search_age_10_btn);
		member_search_age_20_btn = (Button) findViewById(R.id.member_search_age_20_btn);
		member_search_age_30_btn = (Button) findViewById(R.id.member_search_age_30_btn);
		member_search_age_40_btn = (Button) findViewById(R.id.member_search_age_40_btn);
		member_search_age_50_btn = (Button) findViewById(R.id.member_search_age_50_btn);
		member_search_age_60_btn = (Button) findViewById(R.id.member_search_age_60_btn);

		memberIdSearchText = (EditText)findViewById(R.id.member_id_search_text);
		memberNickNameSearchText = (EditText)findViewById(R.id.member_nickname_search_text);

		// 상단 닫기 버튼
		topNaviCloseBtn = (Button) findViewById(R.id.top_navi_close_btn);
		topNaviCloseBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// 하단 조회 버튼
		callSearchBtn = (Button) findViewById(R.id.call_search_btn);
		// 하단 조회 버튼 리스너
		callSearchBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 키,벨류를 넣는다
				extraBundle.putString("searchMemberId",  (memberIdSearchText.getText()+""));		// 아이디
				extraBundle.putString("searchNickName",  (memberNickNameSearchText.getText()+""));	// 닉네임
				extraBundle.putString("searchGender",  selectedGender);								// 성별
				extraBundle.putString("searchAge",  	selectedAge);								// 나이
				extraBundle.putString("searchPersonalityType",  "");								// 성격

				// 인텐트에 넣는다
				resultIntent.putExtras(extraBundle);
				callSetResult();
			}
		});

		// 데이터 전달용 번들을 만든다. 
		extraBundle = new Bundle();
		resultIntent = new Intent();

		// 초기값 설정  - 사용자가 그냥 검색 버튼 누르면 기본 조건으로 검색
		//		extraBundle.putString("searchAllYn", "Y");					// 전체 검색
		this.setResult(RESULT_CANCELED, resultIntent);	// RESULT_OK 를 돌려주면 부모에서 받아 처리한다.

	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////	

	/**
	 * 버튼 리스너
	 */
	public void memberSearchOnClickListener(View v) {
		hideKeyboard();
		switch(v.getId()) {
		case R.id.member_search_gender_all_btn:
			selectedGender = (String) v.getTag();
			member_search_gender_all_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_gender_male_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_female_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_etc_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_gender_male_btn:
			member_search_gender_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_male_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_gender_female_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_etc_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			selectedGender = (String) v.getTag();
			break;
		case R.id.member_search_gender_female_btn:
			member_search_gender_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_male_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_female_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_gender_etc_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			selectedGender = (String) v.getTag();
			break;
		case R.id.member_search_gender_etc_btn:
			member_search_gender_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_male_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_female_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_gender_etc_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			selectedGender = (String) v.getTag();
			break;

		case R.id.member_search_age_all_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_10_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_20_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_30_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_40_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_50_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			break;
		case R.id.member_search_age_60_btn:
			selectedAge = (String) v.getTag();
			member_search_age_all_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_10_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_20_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_30_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_40_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_50_btn.setTextColor(Color.parseColor(COLOR_NOT_SELECTED));
			member_search_age_60_btn.setTextColor(Color.parseColor(COLOR_SELECTED));
			break;
		}
	}

	//	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * businessKindInitColor
	 *  그리드뷰 초기 선택값 배경색.(전체 버튼)
	 *
	 * @param 
	 * @param 
	 * @return 
	 */
	public void businessKindInitColor(){
		Log.d(TAG, "businessKindInitColor");
		if(!isFinished){
			runOnUiThread(new Runnable(){		
				@Override
				public void run(){
					Message message = handler.obtainMessage();
					Bundle b = new Bundle();
					b.putInt("businessKindInitColor", 1);		 
					message.setData(b);
					handler.sendMessage(message);
				}
			});
		}
	}
	/**
	 * gridViewChangeBackgroundColor
	 *  그리드뷰 색상 변경하기
	 *
	 * @param 
	 * @param 
	 * @return 
	 */
	public void gridViewChangeBackgroundColor(final int idx){
		Log.d(TAG, "gridViewChangeBackgroundColor");
		if(!isFinished){
			runOnUiThread(new Runnable(){		
				@Override
				public void run(){
					Message message = handler.obtainMessage();
					Bundle b = new Bundle();
					b.putInt("gridViewChangeBackgroundColor", 1);		 
					b.putInt("idx", idx);		 
					message.setData(b);
					handler.sendMessage(message);
				}
			});
		}
	}

	/**
	 * hideKeyboard
	 * 키보드 숨긴다
	 *
	 * @param 
	 * @param 
	 * @return nowTime
	 */
	public void hideKeyboard(){
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE );
		imm .hideSoftInputFromWindow(memberIdSearchText.getWindowToken(), 0);
	}

	public Context returnThis(){
		return this;
	}

	public void callSetResult(){
		this.setResult(RESULT_OK, resultIntent);	// RESULT_OK 를 돌려주면 부모에서 받아 처리한다.
		finish();
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
	}


}